package com.ruoyi.framework.xa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;


/**
 * @Author alison
 * @Date 2024/12/13 21:07
 * @Version 1.0
 * @Description
 */
@Component
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class ScriptRunner implements ApplicationRunner {

    @Qualifier("sJdbcTemplate")
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Qualifier("mJdbcTemplate")
    @Autowired
    private JdbcTemplate mjdbcTemplate;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String sqlPath = "sql/schema.sql";
        ClassPathResource classPathResource = new ClassPathResource(sqlPath);
        String string = new String(FileCopyUtils.copyToByteArray(classPathResource.getInputStream()));
        string = string.replace(";","").replaceAll("\\r\\n", "");
        System.out.println(string);
        jdbcTemplate.execute(string);
        mjdbcTemplate.execute(string);
    }
}
